package com.qiantu.phone.ui.activity;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.PanelBindConfig;
import com.qiantu.api.entity.PanelBindStatusBean;
import com.qiantu.api.entity.SceneBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Log;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.fragment.SceneFragment;
import com.qiantu.phone.ui.fragment.SmartAppliancesFragment;
import com.qiantu.phone.ui.uiutil.PanelBindConfigUIUtil;
import com.qiantu.phone.widget.OperateBindView;

import java.util.ArrayList;
import java.util.List;

public class PanelBindSelectActivity extends AppActivity {
    @Log
    public static void start(Context context, PanelBindConfig panelBindConfig) {
        Intent intent = new Intent(context, PanelBindSelectActivity.class);
        intent.putExtra("panelBindConfig", panelBindConfig);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    private SmartAppliancesFragment mSmartAppliancesFragment;
    private SceneFragment mSceneFragment;
    private PanelBindConfigUIUtil panelBindConfigUIUtil;
    private OperateBindView operateBindView;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_panel_bind_select;
    }

    @Override
    protected void initView() {
        PanelBindConfig panelBindConfig = (PanelBindConfig) getIntent().getSerializableExtra("panelBindConfig");
        panelBindConfigUIUtil = new PanelBindConfigUIUtil(getContext(), panelBindConfig);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (panelBindConfigUIUtil.isSceneItem()){
            mSceneFragment = SceneFragment.newInstance(true, true, SceneFragment.SELECT_SCENE);
            mSceneFragment.setSingleSelect(true);
            fragmentTransaction.add(R.id.fragment, mSceneFragment);
        }else {
            if(!TextUtils.isEmpty(panelBindConfigUIUtil.getDeviceMainType())){
                mSmartAppliancesFragment = SmartAppliancesFragment.newInstance(SmartAppliancesFragment.SearchType.SEARCH_TYPE_DEVICETYPE,panelBindConfigUIUtil.getDeviceMainType());
            }else{
                mSmartAppliancesFragment = SmartAppliancesFragment.newInstance(SmartAppliancesFragment.SearchType.SEARCH_TYPE_ALL,panelBindConfigUIUtil.getDeviceMainType());
            }
            mSmartAppliancesFragment.setSelectable(true);
            mSmartAppliancesFragment.setSingleSelect(true);
            fragmentTransaction.add(R.id.fragment, mSmartAppliancesFragment);
        }
        fragmentTransaction.commitNowAllowingStateLoss();

        operateBindView = findViewById(R.id.operate_bind_view);
        operateBindView.setInfoBean(null);
        operateBindView.setBindType(1);
        operateBindView.setCallback(new OperateBindView.Callback() {
            @Override
            public void requestInit(String statusKey, String statusValueName, Object statusValue, int bindActionType, int actionType) {
                LLHttpManager.panelBind(getAppActivity(), panelBindConfigUIUtil.config, new HttpCallback<HttpData<PanelBindConfig>>(getAppActivity()){
                    @Override
                    public void onSucceed(HttpData<PanelBindConfig> result) {
                        super.onSucceed(result);
                        //拿到CoreId后开始轮询等待服务器返回绑定成功 10秒后失败 3秒请求一次
                        operateBindView.startWait10SecondAnimator(result.getData().getSerialNo());
                    }
                });
            }

            @Override
            public void requestInitQuery(String coreId) {
                LLHttpManager.checkPanelBindingStatus(getAppActivity(), 1, coreId, new HttpCallback<HttpData<PanelBindStatusBean>>(getAppActivity()){
                    @Override
                    public void onSucceed(HttpData<PanelBindStatusBean> result) {
                        super.onSucceed(result);
                        if (result.getData().status == 1){
                            operateBindView.bindComplete(new OperateBindView.DelayTimeCallBack() {
                                @Override
                                public void callback(int time) {
                                    startActivity_SingleTask(PanelBindConfigActivity.class);
                                }
                            });
                        }
                    }
                });
            }
        });
    }

    @Override
    protected void initData() {
        if (panelBindConfigUIUtil.isSceneItem()){
            List<String> selectedScenes = new ArrayList<>();
            if (panelBindConfigUIUtil.config.getBindSateType() == 2 && panelBindConfigUIUtil.sceneBean != null){
                selectedScenes.add(panelBindConfigUIUtil.sceneBean.getSceneSerialNo());
            }
            if (selectedScenes.size()>0){
                getTitleBar().getRightView().setEnabled(true);
                mSceneFragment.setSelectedScenes(selectedScenes);
            }else {
                getTitleBar().getRightView().setEnabled(false);
            }

        }else {
            List<DeviceBean> selectedDevices = new ArrayList<>();
            if (panelBindConfigUIUtil.config.getBindSateType() == 2 && panelBindConfigUIUtil.deviceBean != null){
                selectedDevices.add(panelBindConfigUIUtil.deviceBean);
            }
            if(selectedDevices != null && selectedDevices.size() > 0){
                mSmartAppliancesFragment.checkSelected(selectedDevices);
                getTitleBar().getRightView().setEnabled(true);
            }else{
                getTitleBar().getRightView().setEnabled(false);
            }
        }
    }

    public void onSelectedDevices(ArrayList<DeviceBean> selectedDevices){
        if(selectedDevices == null || selectedDevices.size() == 0){
            getTitleBar().getRightView().setEnabled(false);
        }else{
            getTitleBar().getRightView().setEnabled(true);
        }
    }

    @Override
    public void onRightClick(View view) {
        if (panelBindConfigUIUtil.isSceneItem()){
            SceneBean sceneBean = mSceneFragment.getSelectedScenes().get(0);
            panelBindConfigUIUtil.config.setBindSerialNo(sceneBean.getSceneSerialNo());
            panelBindConfigUIUtil.config.setBindType(7);
        }else {
            DeviceBean deviceBean = mSmartAppliancesFragment.getSelectedDevices().get(0);
            panelBindConfigUIUtil.config.setBindSerialNo(deviceBean.getDeviceSerialNo());
            int bindType = panelBindConfigUIUtil.getDeviceBindType(deviceBean);
            panelBindConfigUIUtil.config.setBindType(bindType);
        }
        operateBindView.setVisibility(View.VISIBLE);
    }
}